const connection = require('../app/database');

class DiscussService {
  // 创建一条评论
  async create(userId, content, articleId) {
    try {
      const statement = `INSERT INTO discuss (user_id, content, article_id) VALUES (?, ?, ?)`;
      const result = await connection.execute(statement, [userId, content, articleId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 回复一条评论
  async reply(userId, content, articleId, discussId) {
    try {
      const statement = `INSERT INTO discuss (user_id, content, article_id, discuss_id) VALUES (?, ?, ?, ?)`;
      const result = await connection.execute(statement, [userId, content, articleId, discussId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 删除一条评论
  async remove(userId, discussId) {
    try {
      const statement = `DELETE FROM discuss WHERE user_id = ? AND id = ?`;
      const result = await connection.execute(statement, [userId, discussId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 获取评论列表
  async list(articleId, offset, limit) {
    try {
      const statement = `
                        SELECT 
                          d.id id,
                          d.content content,
                          d.article_id articleId,
                          d.discuss_id discussId,
                          d.likes likes,
                          d.createAt createTime,
                          d.updateAt updateTime,
                          (SELECT JSON_ARRAYAGG(ds.user_id) FROM discuss_support ds WHERE d.id = ds.discuss_id) likeByUserId,
                          JSON_OBJECT('id', u.id, 'nickname', u.nickname, 'avatarUrl', u.avatar_url, 'isPrivate', u.private, 'createTime', u.createAt) author,
                          (SELECT 
                            JSON_ARRAYAGG(
                              JSON_OBJECT(
                                'id', dd.id, 
                                'content', dd.content, 
                                'articleId', dd.article_id, 
                                'discussId', dd.discuss_id,
                                'likes', dd.likes,
                                'likeByUserId', (SELECT JSON_ARRAYAGG(dss.user_id) FROM discuss_support dss WHERE dd.id = dss.discuss_id),
                                'author', JSON_OBJECT('id', uu.id, 'nickname', uu.nickname, 'avatarUrl', uu.avatar_url, 'isPrivate', uu.private, 'createTime', uu.createAt),
                                'createTime', dd.createAt,
                                'updateTime', dd.updateAt
                              )
                            ) 
                            FROM discuss dd 
                            LEFT JOIN user uu ON uu.id = dd.user_id
                            WHERE dd.discuss_id = d.id
                          ) 
                          childDiscuss
                        FROM discuss d
                        LEFT JOIN user u ON u.id = d.user_id
                        WHERE d.article_id = ? AND ISNULL(d.discuss_id)
                        LIMIT ?, ?`;
      const result = await connection.execute(statement, [articleId, offset, limit]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

}

module.exports = new DiscussService();
